package com.baitengsoft.patentapimonitor.common.utils;

import java.io.Serializable;
import java.util.List;

/**
 * 分页工具类
 * @author Chen 2017/12/18
 */
public class PageResult<T> implements Serializable {
    private int pageSize;   //每页多少条数据
    private int pageNumber; //当前页号
    private int pagesCount; //一共有多少页数据
    private int totalItemsCount;    //数组总条数
    private List<T> items;  //数据

    public PageResult(List<T> items, int pageNumber, int pageSize, int totalItemsCount) {
        if(pageNumber < 1) {
            throw new IllegalArgumentException("pageNumber 参数不能小于 1。");
        } else if(pageSize < 0) {
            throw new IllegalArgumentException("pageSize 参数不能小于 0。");
        } else if(totalItemsCount < 0) {
            throw new IllegalArgumentException("totalItemsCount 参数不能小于 0。");
        } else {
            this.pageNumber = pageNumber;
            this.pageSize = pageSize;
            this.pagesCount = this.calcPageCount(pageSize, totalItemsCount);
            this.totalItemsCount = totalItemsCount;
            this.items = items;
        }
    }
    private int calcPageCount(int size, int itemsCount) {
        return (int)Math.ceil((double)itemsCount / (double)size);
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNumber() {
        return pageNumber;
    }

    public void setPageNumber(int pageNumber) {
        this.pageNumber = pageNumber;
    }

    public int getPagesCount() {
        return pagesCount;
    }

    public void setPagesCount(int pagesCount) {
        this.pagesCount = pagesCount;
    }

    public int getTotalItemsCount() {
        return totalItemsCount;
    }

    public void setTotalItemsCount(int totalItemsCount) {
        this.totalItemsCount = totalItemsCount;
    }

    public List<T> getItems() {
        return items;
    }

    public void setItems(List<T> items) {
        this.items = items;
    }
}
